package youcaihuaoa.weetop.com.ui.adapter

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.robot.base.model.QxsxDataBean
import youcaihuaoa.weetop.com.R

class TreeViewAdapter1(var parentIndex: Int = 0) :
    BaseQuickAdapter<QxsxDataBean, BaseViewHolder>(R.layout.app_item_tree_view) {

    var onItemClick: OnItemClick? = null

    override fun convert(holder: BaseViewHolder, item: QxsxDataBean) {
        val disclosureImg = holder.getView<ImageView>(R.id.disclosureImg)
        val contentText = holder.getView<TextView>(R.id.contentText)
        val iv_select = holder.getView<ImageView>(R.id.iv_select)
        var sel = false
        item.son_menu_data?.forEach {
            if (it.select) {
                sel = true
            }
        }
        if (!item.son_menu_data.isNullOrEmpty()) {
            item.select = sel
        }
        iv_select.isSelected = item.select
        iv_select.setOnClickListener {
            if(getOnItemClickListener() != null){
                getOnItemClickListener()?.onItemClick(this, it, holder.adapterPosition)
            }
        }
        contentText.text = item.name
        iv_select.isEnabled = item.enable
        val item_mRv = holder.getView<RecyclerView>(R.id.item_mRv)
        val mTreeAdapter = TreeViewAdapter2().apply {
            setOnItemClickListener { _, view, position ->
                val mItemBean = (this.data[position] as QxsxDataBean)
                mItemBean.expanded = !mItemBean.expanded
                if (mItemBean.son_menu_data.isNullOrEmpty()) {
                    mItemBean.select = !mItemBean.select
                }
                LogUtils.e(parentIndex)
                onItemClick?.onItem(parentIndex)
                this.notifyItemChanged(position)
                this@TreeViewAdapter1.notifyDataSetChanged()
            }
        }

        item_mRv.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = mTreeAdapter
        }
        mTreeAdapter.setList(item.son_menu_data)

        if (item.son_menu_data.isNullOrEmpty()) {
            //这里要主动设置一下icon可见，因为convertView有可能是重用了"设置了不可见"的view，下同。
            disclosureImg.setImageResource(R.mipmap.app_ic_treen_close)
            disclosureImg.visibility = View.INVISIBLE
            item_mRv.visibility = View.GONE
        } else {
            if (item.expanded) {
                disclosureImg.setImageResource(R.mipmap.app_ic_treen_open)
                disclosureImg.visibility = View.VISIBLE
                item_mRv.visibility = View.VISIBLE
            } else {
                disclosureImg.setImageResource(R.mipmap.app_ic_treen_close)
                disclosureImg.visibility = View.VISIBLE
                item_mRv.visibility = View.GONE
            }
        }
    }

    interface OnItemClick {
        fun onItem(position: Int)
    }
}